package com.smfx.yxt.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.daoSqlserver.LateCoachPlanMapper;
import com.smfx.yxt.daoSqlserver.LateCoachRegisterMapper;
import com.smfx.yxt.model.LateCoachPlan;
import com.smfx.yxt.model.LateCoachRegister;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.LateCoachQueryVO;
import com.smfx.yxt.service.ILateCoachService;
import com.smfx.yxt.utils.StringUtil;
import com.smfx.yxt.view.LateCoachPlanView;
import com.smfx.yxt.view.LateCoachRegTotalView;
import com.smfx.yxt.view.LateCoachRegView;

@Service("lateCoachService")
public class LateCoachServiceImpl implements ILateCoachService {

	@Autowired
	private LateCoachPlanMapper lateCoachPlanMapper;
	@Autowired
	private LateCoachRegisterMapper lateCoachRegisterMapper;
	
	@Override
	public int addLateCoachPlan(LateCoachPlan record) {
		return lateCoachPlanMapper.insertSelective(record);
	}

	@Override
	public LateCoachPlan selectPlanById(Integer id) {
		return lateCoachPlanMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateLateCoachPlan(LateCoachPlan record) {
		return lateCoachPlanMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<LateCoachPlanView> selectTodayLateCoachList(LateCoachQueryVO vo) {
		return lateCoachPlanMapper.selectTodayLateCoachList(vo);
	}

	@Override
	public List<LateCoachPlanView> selectListByPage(Page<LateCoachPlan> page) {
		return lateCoachPlanMapper.selectListByPage(page);
	}

	@Override
	public int insertPlanBatch(List<LateCoachPlan> list) {
		List<LateCoachPlan> insertList = new ArrayList<>();
		for(int i = 0; i < list.size(); i++) {
			insertList.add(list.get(i));
			if(i % 99 == 0) {
				lateCoachPlanMapper.insertPlanBatch(insertList);
				insertList = new ArrayList<>();
			}
		}
		if(!StringUtil.isNullOrEmpty(insertList)) {
			lateCoachPlanMapper.insertPlanBatch(insertList);
			insertList = new ArrayList<>();
		}
		return 1;
	}

	@Override
	public RspResult addLateCoachRegister(LateCoachRegister record) {
		String[] teachers = record.getTeacherName().split(",");
		List<String> tipList = new ArrayList<>();
		for(String teacherName : teachers) {
			LateCoachQueryVO vo = new LateCoachQueryVO();
			vo.setCoachType(record.getCoachType());
			vo.setTeacherName(teacherName);
			LateCoachRegister regRecord= lateCoachRegisterMapper.selectTodayReg(vo);
			if(regRecord == null) {
				LateCoachRegister insert = new LateCoachRegister();
				insert.setTermId(record.getTermId());
				insert.setWeek(record.getWeek());
				insert.setClassId(record.getClassId());
				insert.setCoachType(record.getCoachType());
				insert.setTeacherName(teacherName);
				lateCoachRegisterMapper.insertSelective(insert);
			}else {
				if(record.getClassId() == null) tipList.add(teacherName + "教师已登记了");
				else tipList.add(teacherName + "教师已在" + regRecord.getClassId() + "班登记了");
			}
		}
		return new RspResult(Status.SUCCESS, String.join(",", tipList));
	}
	
	@Override
	public LateCoachRegister selectRegisterById(Integer id) {
		return lateCoachRegisterMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateLateCoachRegister(LateCoachRegister record) {
		return lateCoachRegisterMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<LateCoachRegister> selectTodayNoComeList(LateCoachQueryVO vo) {
		return lateCoachRegisterMapper.selectTodayNoComeList(vo);
	}

	@Override
	public List<LateCoachRegTotalView> selectRegTotalList(LateCoachQueryVO vo) {
		List<LateCoachRegView> list = lateCoachRegisterMapper.selectRegTotalList(vo);
		List<LateCoachRegTotalView> totalList = new ArrayList<>();
		Map<Integer, LateCoachRegTotalView> map = new LinkedHashMap<>();
		for(LateCoachRegView reg : list) {
			LateCoachRegTotalView totalView = map.get(reg.getTeacherId());
			if(totalView == null) {
				totalView = new LateCoachRegTotalView();
				totalView.setTeacherId(reg.getTeacherId());
				totalView.setTeacherName(reg.getTeacherName());
				totalView.setClassTeacher(reg.getBzId() != null ? true : false);
				totalView.setGrade(reg.getGrade());
				totalView.setStudyType(reg.getStudyType());
				map.put(reg.getTeacherId(), totalView);
			}
			if(reg.getWeek() == 1) totalView.setD1Count(reg.getWeekCount());
			if(reg.getWeek() == 2) totalView.setD2Count(reg.getWeekCount());
			if(reg.getWeek() == 3) totalView.setD3Count(reg.getWeekCount());
			if(reg.getWeek() == 4) totalView.setD4Count(reg.getWeekCount());
			if(reg.getWeek() == 5) totalView.setD5Count(reg.getWeekCount());
			if(reg.getWeek() == 6) totalView.setD6Count(reg.getWeekCount());
			if(reg.getWeek() == 7) totalView.setD7Count(reg.getWeekCount());
			if(reg.getWeek() == 8) totalView.setD8Count(reg.getWeekCount());
		}
		for (LateCoachRegTotalView value : map.values()) {
			totalList.add(value);
		}
		return totalList;
	}

}
